package spider.hysrlzy;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import spider.Capturer;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class JobDataCapturer implements Capturer {
    @Override
    public Map<String, String> capture(String url) {
        try {
            Map<String, String> ret = new HashMap<>();
            URL u = new URL(url);
            Document doc = null;
            doc = Jsoup.parse(u, 5000);
            Elements ele = doc.select(".jobdetail li,.jobdetail p");
            if(ele.size()>0){
                for (int i = 0; i < ele.size(); i++) {
                    String text = ele.get(i).text();
                    String[] items = text.split("：");
                    if (items.length > 1) {
                        ret.put(items[0], items[1]);
                    }
                }
                ele = doc.select(".jobName>.jobName1,.jobName>.jobName2");
                if(ele.size()==2){
                    ret.put("job_name", ele.get(0).text());
                    ret.put("company", ele.get(1).text());
                }
            }
            return ret;
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 出错返回空集合
        return Collections.emptyMap();
    }
}
